﻿using ProTrack.Dto;
using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IRepository.WareHouse;
using ProTrack.IServices.Stock;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.Stock
{
    /// <summary>
    /// 仓库调拨业务逻辑层
    /// </summary>
    public class WareHouseAllocationServices : IWareHouseAllocationServices
    {
        public WareHouseAllocationServices(IBaseRepository db, IWareHouseAllocationRepository or) 
        {
            OR = or;
            DB = db;
        }

        private IWareHouseAllocationRepository OR { get; }
        private IBaseRepository DB;

        public async Task<bool> AddAsync(WareHouseAllocation model, List<WareHouseAllocationItem> list)
        {
            var result = await OR.AddAsync(model, list);
            return result;
        }

        public async Task<bool> DeleteAsync(int[] Ids)
        {
            var result = await OR.DeleteAsync(Ids);
            return result;
        }

        public async Task<bool> DeleteAsync(int Id)
        {
            var result = await OR.DeleteAsync(Id);
            return result;
        }

        public async Task<IEnumerable<WareHouseAllocation>> GetAllAsync()
        {
            string sql = "select * from WareHouseAllocation";
            var list = await DB.QueryAllAsync<WareHouseAllocation>(sql);
            return list;
        }

        public async Task<IEnumerable<WareHouseAllocation>> GetAsync(int PageIndex, int PageSize, string Number, string EmployeesName, string ExWareHouseName, int? Status)
        {
            string sql = "select * from WareHouseAllocation where 1 = 1";
            if(!string.IsNullOrEmpty(Number)) 
            {
                sql += " and Number like @Number";
            }
            if(!string.IsNullOrEmpty(EmployeesName)) 
            {
                sql += " and EmployeesName like @EmployeesName";
            }
            if(!string.IsNullOrEmpty(ExWareHouseName))
            {
                sql += " and ExWareHouseName like @ExWareHouseName";
            }
            if (!string.IsNullOrEmpty(Status.ToString()))
            {
                sql += " and Status like @Status";
            }
            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only";
            var list = await DB.QueryAsync<WareHouseAllocation>(sql, new { PageIndex,PageSize, Number = $"%{Number}%" , EmployeesName = $"%{EmployeesName}%", ExWareHouseName = $"%{ExWareHouseName}%", Status = $"%{Status}%" });
            return list;
        }

        public async Task<int> GetCountAsync(string Number, string EmployeesName, string ExWareHouseName, int? Status)
        {
            string sql = "select count(*) from WareHouseAllocation where 1 = 1";
            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number";
            }
            if (!string.IsNullOrEmpty(EmployeesName))
            {
                sql += " and EmployeesName like @EmployeesName";
            }
            if (!string.IsNullOrEmpty(ExWareHouseName))
            {
                sql += " and ExWareHouseName like @ExWareHouseName";
            }
            if (!string.IsNullOrEmpty(Status.ToString()))
            {
                sql += " and Status like @Status";
            }
            var count = await DB.GetRecoreCountAsync(sql, new { Number = $"%{Number}%", EmployeesName = $"%{EmployeesName}%", ExWareHouseName = $"%{ExWareHouseName}%", Status = $"%{Status}%" });
            return count;
        }

        public async Task<bool> UpdateAsync(WareHouseAllocation model, List<WareHouseAllocationItem> list)
        {
            var result = await OR.UpdateAsync(model, list);
            return result;
        }

        public async Task<bool> UpdateStatus(long Id, int Status)
        {
            string sql = "update WareHouseAllocation set Status = @Status where Id = @Id";
            var result = await DB.UpdateAsync(sql, new { Id, Status });
            return result;
        }

        public async Task<IEnumerable<WareHouseAllocationItemDto>> GetWareHouseAllocationIdAsync(string WareHouseAllocationId, string WareHouseId)
        {
            var list = await DB.QueryAsync<WareHouseAllocationItemDto>("SELECT WI.ItemId,i.ItemName,i.Spec,i.UnitOfMeasure,(ik.StockCount - ik.SalesCount) AS CurrCount,WI.Amount,WI.TaxPrice,WI.TaxAmount,WI.Count,WI.Price,WI.Tax,WI.Remark,ik.WareHouseId FROM Item i INNER JOIN ItemStock ik ON i.Id = ik.ItemId AND ik.WareHouseId = @WareHouseId INNER JOIN WareHouseAllocationItem WI ON i.Id = WI.ItemId WHERE WI.WareHouseAllocationId = @WareHouseAllocationId", new { WareHouseAllocationId, WareHouseId });
            return list;
        }
    }
}
